# @before-stub-for-debug-begin
from python3problem501 import *
from typing import *
# @before-stub-for-debug-end

#
# @lc app=leetcode.cn id=501 lang=python3
#
# [501] 二叉搜索树中的众数
#
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
from typing import List
# @lc code=start
# Definition for a binary tree node.
class Solution:
    max_freq = 0
    def findMode(self, root: TreeNode) -> List[int]:
        if root is None:
            return []
        d = dict()
        self.inorder(root, d)
        ans = []
        for key, val in d.items():
            if val == self.max_freq:
               ans.append(key)
        return ans 


    def inorder(self, root, d):
        if root is None:
            return
        self.inorder(root.left, d)
        d[root.val] = d.get(root.val, 0) + 1
        self.max_freq = max(self.max_freq, d[root.val])
        self.inorder(root.right, d)
# @lc code=end

